using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Xml.Serialization;
using System.IO;
using PluginHandler;
using System.Security.Cryptography;

namespace DatabaseManager.StoredConnections
{
    /// <summary>
    /// Holds and manages all configuration files
    /// </summary>
    public class StoredConnectionManager
    {
        private List<StoredConnectionFile> _storedConnectionFiles;
        private StoredConnectionFile _defaultConnectionFile;

        /// <summary>
        /// Initializes a new instance of the <see cref="StoredConnectionManager"/> class, which reads all stored configurations from the xml file.
        /// </summary>
        public StoredConnectionManager()
        {
            _storedConnectionFiles = new List<StoredConnectionFile>();
        }

        /// <summary>
        /// Saves all changes in the choosen xml files
        /// </summary>
        public void SaveAll()
        {
            foreach (StoredConnectionFile storedConnectionFile in _storedConnectionFiles)
            {
                if (storedConnectionFile.IsEditable)
                {
                    storedConnectionFile.Save();
                }
            }
        }

        /// <summary>
        /// Loads all choosen xml files
        /// </summary>
        public void LoadAll()
        {
            foreach (StoredConnectionFile storedConnectionFile in _storedConnectionFiles)
            {
                storedConnectionFile.Load();
            }
        }

        /// <summary>
        /// Gets a list of the stored connections in the choosen xml file
        /// </summary>
        /// <value>The stored connections.</value>
        public List<StoredConnectionFile> StoredConnectionFiles
        {
            get
            {
                return _storedConnectionFiles;
            }
        }

        /// <summary>
        /// Gets or sets the default stored connection file, in which new configuration entries are added.
        /// </summary>
        /// <value>The default stored connection file.</value>
        public StoredConnectionFile DefaultStoredConnectionFile
        {
            get { return _defaultConnectionFile; }
            set { _defaultConnectionFile = value; }
        }

        /// <summary>
        /// Retrieves the editable state of the configuration file in which the given stored connection is included
        /// </summary>
        /// <returns></returns>
        public bool GetEditableStateForStoredConnection(StoredConnection storedConnection)
        {
            StoredConnectionFile storedConnectionFile = GetStoredConnectionFileFromStoredConnection(storedConnection);

            if (storedConnectionFile != null)
            {
                return storedConnectionFile.IsEditable;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Gets the stored connection file from the stored connection.
        /// </summary>
        /// <param name="storedConnection">The stored connection.</param>
        /// <returns></returns>
        public StoredConnectionFile GetStoredConnectionFileFromStoredConnection(StoredConnection storedConnection)
        {
            foreach (StoredConnectionFile storedConnectionFile in _storedConnectionFiles)
            {
                if (storedConnectionFile.StoredConnections.Contains(storedConnection))
                {
                    return storedConnectionFile;
                }
            }

            return null;
        }
    }
}
